﻿package com.thorqq.magictimer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.thorqq.magictimer.core.TTimerLog;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.BaseAdapter;

public class AdapterTimerLog extends BaseAdapter
{
    public final class ViewHolder
    {
        public TextView start_time;
        public TextView timer_name;
        public TextView action_name;
    }

    private LayoutInflater mInflater;
    private List<Map<String, Object>> mData;

    public AdapterTimerLog(Context context)
    {
        mInflater = LayoutInflater.from(context);
        mData = new ArrayList<Map<String, Object>>();
    }

    public AdapterTimerLog(Context context, List<Map<String, Object>> data)
    {
        mInflater = LayoutInflater.from(context);
        mData = data;
    }

    public void add(Map<String, Object> map)
    {
        mData.add(map);
    }

    public void removeItem(int position)
    {
        mData.remove(position);
    }

    public void updateItem(int position, String key, Object obj)
    {
        if (position < mData.size())
        {
            mData.get(position).put(key, obj);
        }
    }

    public int getCount()
    {
        return mData.size();
    }

    public Object getItem(int position)
    {
        if (position < mData.size())
        {
            return mData.get(position);
        }
        else
        {
            return null;
        }
    }

    public long getItemId(int position)
    {
        if (position < mData.size())
        {
            return position;
        }
        else
        {
            return -1;
        }
    }
    
    public View getView(final int position, View convertView, ViewGroup parent)
    {        
        ViewHolder holder = null;
        if (convertView == null)
        {
            holder = new ViewHolder();

            convertView = mInflater.inflate(R.layout.timerlog_item, null);
            holder.start_time = (TextView) convertView.findViewById(R.id.tvTimerLogStartTime);
            holder.timer_name = (TextView) convertView.findViewById(R.id.tvTimerLogTimerName);
            holder.action_name = (TextView) convertView.findViewById(R.id.tvTimerLogActionName);

            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        
        TTimerLog timerLog = (TTimerLog) mData.get(position).get("timerLog");
        holder.start_time.setText(timerLog.start_time);
        holder.timer_name.setText(timerLog.timer_name);
        holder.action_name.setText(timerLog.action_name);
                
        return convertView;
    }


}
